home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright (c) 1999-2001 Alex Belgraver (http://www.belgraver.demon.nl)
- Released part of the Currency Converter 2 (GPL) package.
- Version 1.01 April 26, 2001
- */
-
- // factory class
- function CurrencyFactory(){
- this.create=create;
- this.get=get;
- this.getByNr=getByNr;
- this.count=count;
- this.getIterator=getIterator;
-
- this.currencies=new Array();
-
- // add a new currency n=name, r=rate
- // 1 euro = 2.20 dutch guilder... so create("EUR",1) and create("NLG",2.20)
- function create(n,r) {
- this.currencies.push(new Currency(n,r));
- }
- // request a currency by name
- function get(name){
- for (i=0; i<this.currencies.length; i++) {
- if (this.currencies[i].name==name)
- return this.currencies[i];
- }
- return 0;
- }
- // retrieve item by number
- function getByNr(i){
- return this.currencies[i];
- }
- // retrieve number of items
- function count(){
- return this.currencies.length;
- }
- // create Iterator
- function getIterator(){
- return new CurrencyIterator(this);
- }
- }
-
- // entity class for currencies
- function Currency(n,r) {
- this.name=n;
- this.rate=r;
-
- this.convertFrom=convertFrom;
- // convert amount amount from currency curr to this currency
- function convertFrom(curr,amount){
- value=(amount * this.rate) / curr.rate;
- str=(Math.round(value * 100)) / 100; // round at 2 decimals
- return str;
- }
- }
-
- // iterator
- function CurrencyIterator(cf){
- this.first=first;
- this.next=next;
- this.isDone=isDone;
- this.currentItem=currentItem;
-
- this.factory=cf;
- this.counter=0;
-
- function first(){
- this.counter=0;
- }
- function next(){
- this.counter++;
- }
- function isDone(){
- return (this.counter>=cf.count());
- }
- function currentItem(){
- return this.factory.getByNr(this.counter);
- }
- }
-
- function convert(newName, referenceName, referencePrice){
- curr=currFac.get(newName);
- referenceCurr=currFac.get(referenceName);
- var newPrice="Unknown";
- if ((curr!=0) && (referenceCurr!=0)) { // if both exist then
- newPrice=curr.convertFrom(referenceCurr,referencePrice);
- }
- return newPrice;
- }
-
- currFac=new CurrencyFactory();